package net.dgg.zz.credential.certificateMatching.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.api.credential.entity.CertificateProductConfiguration;
import net.dgg.zz.api.credential.mapper.CertificateProductConfigurationMapper;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.credential.certificateMatching.dao.CertificateMatchingDao;
import net.dgg.zz.credential.certificateMatching.entity.CertificateMatchingEntity;
import net.dgg.zz.credential.certificateType.entity.CertificateType;
import net.dgg.zz.credential.certificateType.service.CertificateTypeService;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CertificateMatchingService {
    @Autowired
    private CertificateMatchingDao certificateMatchingDao;
    @Autowired
    private CertificateTypeService certificateTypeService;
    @Autowired
    private TranslateUtil translateUtil;

    @Autowired
    private CertificateProductConfigurationMapper certificateProductConfigurationMapper;

    /**
     * 查询配置列表
     * @return
     */
    @Transactional
    public List<CertificateMatchingEntity> getCertificateMatchingList(Map<String,Object> params){
        String[] column = {"certificateLevel"};
        List<CertificateMatchingEntity> list =  certificateMatchingDao.queryCertificateMatchingList(params);
        List<CertificateMatchingEntity> newList = new ArrayList<>();
        //处理转换证书类型，证书专业
        if(list.size()>0){
            for(CertificateMatchingEntity certificateMatchingEntity : list){
                CertificateType certificateType = new CertificateType();
                if(!StringUtils.isEmpty(certificateMatchingEntity.getCertificateType())){
                    certificateType=certificateTypeService.findById(Long.valueOf(certificateMatchingEntity.getCertificateType().toString()));
                    certificateMatchingEntity.setCertificateType(certificateType.getCertificateName());
                }
                if(!StringUtils.isEmpty(certificateMatchingEntity.getCertificateSpecialtys())){
                    String certificateSpecialtys = certificateTypeService.getCerificateTypeName(certificateMatchingEntity.getCertificateSpecialtys());
                    certificateMatchingEntity.setCertificateSpecialtys(certificateSpecialtys);
                }
                newList.add(certificateMatchingEntity);
            }
        }
        return translateUtil.translateList(column, newList);
    }


    /**
     * 更新证书匹配设置
     * @param params
     * @param userEntity
     * @return
     */
    @Transactional
    public Integer updateCertificateMatching(Map params, UserEntity userEntity){
        Map map = new HashMap();
        map.put("parentId",params.get("certificateType"));
        map.put("generalType",params.get("generalType"));
        String ids = null;
        List<String> id = new ArrayList<>();
        if("isAll".equals(params.get("certificateSpecialtys"))) {
            List<Map> mapList = certificateTypeService.getCertificateType(map);
            if(mapList.size()>0) {
                for (Map map1 : mapList) {
                     id.add(map1.get("id").toString());
                }
            }
            ids = String.join(",",id);
        }else{
            ids = params.get("certificateSpecialtys").toString();
        }
        CertificateMatchingEntity certificateMatchingEntity = new CertificateMatchingEntity();
        certificateMatchingEntity.setId(Long.valueOf(params.get("id").toString()));
        certificateMatchingEntity.setCertificateType((String) params.get("certificateType"));
        certificateMatchingEntity.setCertificateSpecialtys(ids);
        certificateMatchingEntity.setCertificateLevel((String) params.get("certificateLevel"));
        certificateMatchingEntity.setMatchingTime(Integer.valueOf(params.get("matchingTime").toString()));
        certificateMatchingEntity.setIsNatureWorkDay(Integer.valueOf(params.get("isNatureWorkDay").toString()));
        certificateMatchingEntity.setPunishCount(Float.valueOf(params.get("punishCount").toString()));
        certificateMatchingEntity.setUnit(Integer.valueOf(params.get("unit").toString()));
        certificateMatchingEntity.setUpdaterUser(userEntity);
        return certificateMatchingDao.updateCertificateMatching(certificateMatchingEntity);
    }

    /**
     * 通过id删除证书匹配
     * @param id
     * @return
     */
    @Transactional
    public Integer deletCertificateMatchingById(Long id){
        return certificateMatchingDao.deleteByPrimaryKey(id);
    }

    /**
     * 通过id查询证书匹配设置
     * @param id
     * @return
     */
    @Transactional
    public CertificateMatchingEntity getCertificateMatchingById(Long id){
        return certificateMatchingDao.selectByPrimaryKey(id);
    }
    @Transactional
    public Integer count(){
        return certificateMatchingDao.count();
    }

    /**
     * 写入配置设置数据
     * @param params
     * @param userEntity
     * @return
     */
    public Integer insert(Map<String,Object> params, UserEntity userEntity){
        Map map = new HashMap();
        map.put("parentId",params.get("certificateType"));
        map.put("generalType",params.get("generalType"));
        Long keyId = DggKeyWorker.nextId();
        String ids = null;
        List<String> id = new ArrayList<>();
        if("isAll".equals(params.get("certificateSpecialtys"))) {
            List<Map> mapList = certificateTypeService.getCertificateType(map);
            if(mapList.size()>0) {
                for (Map map1 : mapList) {
                    id.add(map1.get("id").toString());
                }
            }
            ids = String.join(",",id);
        }else{
            ids = params.get("certificateSpecialtys").toString();
        }
        CertificateMatchingEntity certificateMatchingEntity = new CertificateMatchingEntity();
        certificateMatchingEntity.setId(keyId);
        certificateMatchingEntity.setCertificateType((String) params.get("certificateType"));
        certificateMatchingEntity.setCertificateSpecialtys(ids);
        certificateMatchingEntity.setCertificateLevel((String) params.get("certificateLevel"));
        certificateMatchingEntity.setMatchingTime(Integer.valueOf(params.get("matchingTime").toString()));
        certificateMatchingEntity.setIsNatureWorkDay(Integer.valueOf(params.get("isNatureWorkDay").toString()));
        certificateMatchingEntity.setPunishCount(Float.valueOf(params.get("punishCount").toString()));
        certificateMatchingEntity.setUnit(Integer.valueOf(params.get("unit").toString()));
        certificateMatchingEntity.setUpdaterUser(userEntity);
        certificateMatchingEntity.setCreateUser(userEntity);
        return certificateMatchingDao.insert(certificateMatchingEntity);
    }

    /**
     * 查询配置设置信息
     * @param certificateSpecialtyId
     * @param certificateTypeId
     * @param certificateLevel
     * @return
     */
    public CertificateMatchingEntity getMatchingByMap(Integer certificateSpecialtyId,Integer certificateTypeId,String certificateLevel){
        Map params = new HashMap();
        params.put("certificateSpecialtyId",certificateSpecialtyId);
        params.put("certificateTypeId",certificateTypeId);
        params.put("certificateLevel",certificateLevel);
        return certificateMatchingDao.queryCertificateMatching(params);
    }

    /**
     * 判断是否存在重复配置
     * @param map
     * @return
     */
    public boolean isExist(Map map){
       Integer count = certificateMatchingDao.queryExistCount(map);
       if(count>0){
           return true;
       }else{
           return false;
       }
    }
}
